Online test at Codility. 2 tasks. Test time 140 minutes. Write solution in C.
שאלות מתוך הראיון
Task 2:
You are given a string S consisting of N letters 'a' and/or 'b'. in one
move, you can swap one letter for the other ('a' for 'b' or 'b' for 'a').
Write a function 'solution' that, given a string S, returns the
minimum number of moves required to obtain a string containing no
instances of three identical letters.
Examples:
1. Given S = "baaaaa", the function sould return 1. The string without
three identical consecitive letters which can be obtained in one move
is "baabaa".
2. Given S = "baaabbaabbba", the function sould return 2. There are
four valid strings obtainable in two moves: for example,
"bbaabbaabbaa".
3. Given S = "baabab", the function sould return 0.
Write an efficient algorithm for the following assumptions:
- N is am integer within the range [0..200,000];
- string S consists only of the characters "a" and/or "b".
תשובות
הוסף תשובה
|
לצפיה בתשובות
יוני 2022
לתחזק next לתחילת הרצף הבא, כל איטרציה לחשב את כמות הרצפים חלקי 3
alphabet with only three letters: a,b,c. A string is called diverse if no 3 consecutive letters are the same. a diverse string may not contain any of the strings "aaa", "bbb" or "c cc".
Write a function: char * solution(int A, int 6, int C);
that, given three integers A, B and C, returns any longest possible diverse string containing at most A letters 'a', at most B letters 'b' and at most C letters 'c'. If there is no possibility, return empty string.
Examples:
1.Given A = 6, B = 1 and C = 1, your function may return "aabaacaa". Note that "aacaabaa" would also be a correct answer. return any correct answer.
2 .Given A = 1, B = 3 and C = 1 your function may return "abbcb", "bcbab", "bacbb" or any of several other strings.
3.Given A = 0, B = 1 and C = 8 your function should return "ccbcc", which is the only correct answer in this case.
Assume that:
• A, B and C are integers within the range [0..100];
• A+B+C>0.
In your solution, focus on correctness not performance.
Write a function solution() that, given a string S consisting of N characters, returns the maximum number of letters 'a' that can be inserted into S (including at the front and end of S) so that the resulting string doesn't contain three consecutive letters 'a'. if string S already contains the substring "aaa", then your function should return -1.
שלב ראשון - מבחן ממוחשב שנערך בפלטפורמת Codility שמכיל שתי שאלות. בשביל להמשיך לשלב הבא עליי היה לענות נכונה על שתי השאלות בשעתיים.
שאלות מתוך הראיון
1. במחרוזת נתונה אסור שיהיו 3 או יותר אותיות זהות ברצף. עליך להחזיר מחרוזת מתוקנת. דוגמא: מקבלים aabeeexx, מחזירים aabeexx.
2. נתונה מטריצה של intים בגודל 2*N. בכל אחד מהתאים יכול להיות 0 או 1.
נתון סכום השורה העליונה, סכום השורה התחתונה ומערך של סכומי הטורים.
על סמך הנתונים האלה תמלא את המטריצה כך שסכומי הטורים יהיה נכון וסכומי השורות יהיה נכון ולהחזיר מחרוזת שמייצגת את המטריצה.
דוגמא: סכום שורה עליונה: 3
סכום שורה תחתונה: 2
סכומי הטורים: [2,1,0,1,1]
מחזירים: "11001,10010"